<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>{section},{sectionelse}</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="language.builtin.functions.html" title="Chapter 7. Built-in Functions">
<link rel="prev" href="language.function.php.html" title="{php}">
<link rel="next" href="language.function.setfilter.html" title="{setfilter}">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">{section},{sectionelse}</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="language.function.php.html">Prev</a> </td>
<th width="60%" align="center">Chapter 7. Built-in Functions</th>
<td width="20%" align="right"> <a accesskey="n" href="language.function.setfilter.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="{section},{sectionelse}">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="language.function.section"></a>{section},{sectionelse}</h2></div></div></div>
<p>
  A <code class="varname">{section}</code>
  is for looping over <span class="bold"><strong>sequentially indexed arrays of data</strong></span>,
  unlike <a class="link" href="language.function.foreach.html" title="{foreach},{foreachelse}"><code class="varname">{foreach}</code></a>
  which is used to loop over a
   <span class="bold"><strong>single associative array</strong></span>.
  Every <code class="varname">{section}</code> tag must be paired with
   a closing <code class="varname">{/section}</code> tag.
   </p>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
   The <a class="link" href="language.function.foreach.html" title="{foreach},{foreachelse}"><code class="varname">{foreach}</code></a> loop can do everything a {section} loop can do, and has a simpler and easier syntax. It is usually preferred over the {section} loop.
 </p>
</div>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
   {section} loops cannot loop over associative arrays, they must be numerically indexed, and sequential (0,1,2,...). For associative arrays, use the <a class="link" href="language.function.foreach.html" title="{foreach},{foreachelse}"><code class="varname">{foreach}</code></a> loop.
 </p>
</div>
<div class="informaltable"><table border="1">
<colgroup>
<col align="center">
<col align="center">
<col align="center">
<col align="center">
<col>
</colgroup>
<thead><tr>
<th align="center">Attribute Name</th>
<th align="center">Type</th>
<th align="center">Required</th>
<th align="center">Default</th>
<th>Description</th>
</tr></thead>
<tbody>
<tr>
<td align="center">name</td>
<td align="center">string</td>
<td align="center">Yes</td>
<td align="center"><span class="emphasis"><em>n/a</em></span></td>
<td>The name of the section</td>
</tr>
<tr>
<td align="center">loop</td>
<td align="center">mixed</td>
<td align="center">Yes</td>
<td align="center"><span class="emphasis"><em>n/a</em></span></td>
<td>Value to determine the number of loop iterations</td>
</tr>
<tr>
<td align="center">start</td>
<td align="center">integer</td>
<td align="center">No</td>
<td align="center"><span class="emphasis"><em>0</em></span></td>
<td>The index
      position that the section will begin looping. If the
      value is negative, the start position is calculated
      from the end of the array. For example, if there are
      seven values in the loop array and start is -2, the
      start index is 5. Invalid values (values outside of the
      length of the loop array) are automatically truncated
      to the closest valid value.</td>
</tr>
<tr>
<td align="center">step</td>
<td align="center">integer</td>
<td align="center">No</td>
<td align="center"><span class="emphasis"><em>1</em></span></td>
<td>The step value that will be used to traverse the
      loop array. For example, step=2 will loop on index
      0,2,4, etc. If step is negative, it will step through
      the array backwards.</td>
</tr>
<tr>
<td align="center">max</td>
<td align="center">integer</td>
<td align="center">No</td>
<td align="center"><span class="emphasis"><em>n/a</em></span></td>
<td>Sets the maximum number of times the section
      will loop.</td>
</tr>
<tr>
<td align="center">show</td>
<td align="center">boolean</td>
<td align="center">No</td>
<td align="center"><span class="emphasis"><em><code class="constant">TRUE</code></em></span></td>
<td>Determines whether or not to show this section</td>
</tr>
</tbody>
</table></div>
<p><span class="bold"><strong>Option Flags:</strong></span></p>
<div class="informaltable"><table border="1">
<colgroup>
<col align="center">
<col>
</colgroup>
<thead><tr>
<th align="center">Name</th>
<th>Description</th>
</tr></thead>
<tbody><tr>
<td align="center">nocache</td>
<td>Disables caching of the <code class="varname">{section}</code> loop</td>
</tr></tbody>
</table></div>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem"><p>
   Required attributes are <em class="parameter"><code>name</code></em> and <em class="parameter"><code>loop</code></em>.
   </p></li>
<li class="listitem"><p>
    The <em class="parameter"><code>name</code></em> of the <code class="varname">{section}</code> can be
    anything you like,  made up of letters,  numbers and underscores, like
    <a class="ulink" href="http://php.net/language.variables" target="_top">PHP variables</a>.
    </p></li>
<li class="listitem"><p>
    {section}'s can be nested, and the nested
   <code class="varname">{section}</code> names must be unique from each other.
   </p></li>
<li class="listitem"><p>
   The <em class="parameter"><code>loop</code></em> attribute,
   usually an array of values, determines the number of times the
   <code class="varname">{section}</code> will loop. You can also pass an integer
   as the loop value.
   </p></li>
<li class="listitem"><p>When printing a variable within a <code class="varname">{section}</code>, the
   <code class="varname">{section}</code> <em class="parameter"><code>name</code></em> must be given next
   to variable name within [brackets].
   </p></li>
<li class="listitem"><p>
   <code class="varname">{sectionelse}</code> is
   executed when there are no values in the loop variable.
 	</p></li>
<li class="listitem"><p>
  A <code class="varname">{section}</code> also has its own variables that handle
  <code class="varname">{section}</code> properties.
  These properties are accessible as: <a class="link" href="language.variables.smarty.html#language.variables.smarty.loops" title="{$smarty.section}">
  <em class="parameter"><code>{$smarty.section.name.property}</code></em></a>
  where <span class="quote">“<span class="quote">name</span>”</span> is the attribute <em class="parameter"><code>name</code></em>.
  </p></li>
<li class="listitem"><p>
  <code class="varname">{section}</code> properties are
  <a class="link" href="language.function.section.html#section.property.index" title=".index"><em class="parameter"><code>index</code></em></a>,
  <a class="link" href="language.function.section.html#section.property.index.prev" title=".index_prev"><em class="parameter"><code>index_prev</code></em></a>,
  <a class="link" href="language.function.section.html#section.property.index.next" title=".index_next"><em class="parameter"><code>index_next</code></em></a>,
  <a class="link" href="language.function.section.html#section.property.iteration" title=".iteration"><em class="parameter"><code>iteration</code></em></a>,
  <a class="link" href="language.function.section.html#section.property.first" title=".first"><em class="parameter"><code>first</code></em></a>,
  <a class="link" href="language.function.section.html#section.property.last" title=".last"><em class="parameter"><code>last</code></em></a>,
  <a class="link" href="language.function.section.html#section.property.rownum" title=".rownum"><em class="parameter"><code>rownum</code></em></a>,
  <a class="link" href="language.function.section.html#section.property.loop" title=".loop"><em class="parameter"><code>loop</code></em></a>,
  <a class="link" href="language.function.section.html#section.property.show" title=".show"><em class="parameter"><code>show</code></em></a>,
  <a class="link" href="language.function.section.html#section.property.total" title=".total"><em class="parameter"><code>total</code></em></a>.
  </p></li>
</ul></div>
<div class="example">
<a name="id379165"></a><p class="title"><b>Example 7.63. Looping a simple array with {section}</b></p>
<div class="example-contents">
<p>
<a class="link" href="api.assign.html" title="assign()"><code class="varname">assign()</code></a> an array to Smarty
</p>
<pre class="programlisting">

&lt;?php
$data = array(1000,1001,1002);
$smarty-&gt;assign('custid',$data);
?&gt;

</pre>
<p>The template that outputs the array</p>
<pre class="programlisting">

{* this example will print out all the values of the $custid array *}
{section name=customer loop=$custid}
{section customer $custid} {* short-hand *}
  id: {$custid[customer]}&lt;br /&gt;
{/section}
&lt;hr /&gt;
{*  print out all the values of the $custid array reversed *}
{section name=foo loop=$custid step=-1}
{section foo $custid step=-1} {* short-hand *}
  {$custid[foo]}&lt;br /&gt;
{/section}

  </pre>
<p>
   The above example will output:
  </p>
<pre class="screen">

id: 1000&lt;br /&gt;
id: 1001&lt;br /&gt;
id: 1002&lt;br /&gt;
&lt;hr /&gt;
id: 1002&lt;br /&gt;
id: 1001&lt;br /&gt;
id: 1000&lt;br /&gt;

  </pre>
</div>
</div>
<br class="example-break"><div class="example">
<a name="id379219"></a><p class="title"><b>Example 7.64. {section} without an assigned array</b></p>
<div class="example-contents">
<pre class="programlisting">

{section name=foo start=10 loop=20 step=2}
  {$smarty.section.foo.index}
{/section}
&lt;hr /&gt;
{section name=bar loop=21 max=6 step=-2}
  {$smarty.section.bar.index}
{/section}

</pre>
<p>
  The above example will output:
</p>
<pre class="screen">
  
10 12 14 16 18
&lt;hr /&gt;
20 18 16 14 12 10

  </pre>
</div>
</div>
<br class="example-break"><div class="example">
<a name="id379244"></a><p class="title"><b>Example 7.65. Naming a {section}</b></p>
<div class="example-contents">
<p>The <em class="parameter"><code>name</code></em> of the <code class="varname">{section}</code> can be anything
  you like, see <a class="ulink" href="http://php.net/language.variables" target="_top">PHP variables</a>.
  It is used to reference the data within the <code class="varname">{section}</code>.</p>
<pre class="programlisting">

{section name=anything loop=$myArray}
  {$myArray[anything].foo}
  {$name[anything]}
  {$address[anything].bar}
{/section}

  </pre>
</div>
</div>
<br class="example-break"><div class="example">
<a name="id379282"></a><p class="title"><b>Example 7.66. Looping an associative array with {section}</b></p>
<div class="example-contents">
<p>This is an example of printing an associative array
   of data with a <code class="varname">{section}</code>. Following is the php script to assign the
   <em class="parameter"><code>$contacts</code></em> array to Smarty.</p>
<pre class="programlisting">
  
&lt;?php
$data = array(
          array('name' =&gt; 'John Smith', 'home' =&gt; '555-555-5555',
                'cell' =&gt; '666-555-5555', 'email' =&gt; 'john@myexample.com'),
          array('name' =&gt; 'Jack Jones', 'home' =&gt; '777-555-5555',
                'cell' =&gt; '888-555-5555', 'email' =&gt; 'jack@myexample.com'),
          array('name' =&gt; 'Jane Munson', 'home' =&gt; '000-555-5555',
                'cell' =&gt; '123456', 'email' =&gt; 'jane@myexample.com')
        );
$smarty-&gt;assign('contacts',$data);
?&gt;

  </pre>
<p>The template to output <em class="parameter"><code>$contacts</code></em></p>
<pre class="programlisting">

{section name=customer loop=$contacts}
&lt;p&gt;
  name: {$contacts[customer].name}&lt;br /&gt;
  home: {$contacts[customer].home}&lt;br /&gt;
  cell: {$contacts[customer].cell}&lt;br /&gt;
  e-mail: {$contacts[customer].email}
&lt;/p&gt;
{/section}

  </pre>
<p>
   The above example will output:
  </p>
<pre class="screen">

&lt;p&gt;
  name: John Smith&lt;br /&gt;
  home: 555-555-5555&lt;br /&gt;
  cell: 666-555-5555&lt;br /&gt;
  e-mail: john@myexample.com
&lt;/p&gt;
&lt;p&gt;
  name: Jack Jones&lt;br /&gt;
  home phone: 777-555-5555&lt;br /&gt;
  cell phone: 888-555-5555&lt;br /&gt;
  e-mail: jack@myexample.com
&lt;/p&gt;
&lt;p&gt;
  name: Jane Munson&lt;br /&gt;
  home phone: 000-555-5555&lt;br /&gt;
  cell phone: 123456&lt;br /&gt;
  e-mail: jane@myexample.com
&lt;/p&gt;

  </pre>
</div>
</div>
<br class="example-break"><div class="example">
<a name="id379344"></a><p class="title"><b>Example 7.67. {section} demonstrating the <code class="varname">loop</code> variable</b></p>
<div class="example-contents">
<p>This example assumes that <em class="parameter"><code>$custid</code></em>, <em class="parameter"><code>$name</code></em>
  and <em class="parameter"><code>$address</code></em> are all
  arrays containing the same number of values. First the php script that assign's the
  arrays to Smarty.</p>
<pre class="programlisting">

&lt;?php

$id = array(1001,1002,1003);
$smarty-&gt;assign('custid',$id);

$fullnames = array('John Smith','Jack Jones','Jane Munson');
$smarty-&gt;assign('name',$fullnames);

$addr = array('253 Abbey road', '417 Mulberry ln', '5605 apple st');
$smarty-&gt;assign('address',$addr);

?&gt;

</pre>
<p>
  The <em class="parameter"><code>loop</code></em> variable only determines the number of times to loop.
  You can access ANY variable from the template within the <code class="varname">{section}</code>.
  This is useful for looping multiple arrays. You can pass an array which will determine
  the loop count by the array size, or you can pass an integer to specify the number of loops.
  </p>
<pre class="programlisting">

{section name=customer loop=$custid}
&lt;p&gt;
  id: {$custid[customer]}&lt;br /&gt;
  name: {$name[customer]}&lt;br /&gt;
  address: {$address[customer]}
&lt;/p&gt;
{/section}

  </pre>
<p>
   The above example will output:
  </p>
<pre class="screen">

&lt;p&gt;
  id: 1000&lt;br /&gt;
  name: John Smith&lt;br /&gt;
  address: 253 Abbey road
&lt;/p&gt;
&lt;p&gt;
  id: 1001&lt;br /&gt;
  name: Jack Jones&lt;br /&gt;
  address: 417 Mulberry ln
&lt;/p&gt;
&lt;p&gt;
  id: 1002&lt;br /&gt;
  name: Jane Munson&lt;br /&gt;
  address: 5605 apple st
&lt;/p&gt;

  </pre>
</div>
</div>
<br class="example-break"><div class="example">
<a name="id379428"></a><p class="title"><b>Example 7.68. Nested {section}'s</b></p>
<div class="example-contents">
<p>
  {section}'s can be nested as deep as you like. With nested {section}'s,
  you can access complex data structures, such as multi-dimensional
  arrays. This is an example <code class="filename">.php</code> script thats assign's the arrays.
  </p>
<pre class="programlisting">

&lt;?php

$id = array(1001,1002,1003);
$smarty-&gt;assign('custid',$id);

$fullnames = array('John Smith','Jack Jones','Jane Munson');
$smarty-&gt;assign('name',$fullnames);

$addr = array('253 N 45th', '417 Mulberry ln', '5605 apple st');
$smarty-&gt;assign('address',$addr);

$types = array(
           array( 'home phone', 'cell phone', 'e-mail'),
           array( 'home phone', 'web'),
           array( 'cell phone')
         );
$smarty-&gt;assign('contact_type', $types);

$info = array(
           array('555-555-5555', '666-555-5555', 'john@myexample.com'),
           array( '123-456-4', 'www.example.com'),
           array( '0457878')
        );
$smarty-&gt;assign('contact_info', $info);

?&gt;
  
</pre>
<p>In this template, <span class="emphasis"><em>$contact_type[customer]</em></span> is an array of
  contact types for the current customer.</p>
<pre class="programlisting">

{section name=customer loop=$custid}
&lt;hr&gt;
  id: {$custid[customer]}&lt;br /&gt;
  name: {$name[customer]}&lt;br /&gt;
  address: {$address[customer]}&lt;br /&gt;
  {section name=contact loop=$contact_type[customer]}
    {$contact_type[customer][contact]}: {$contact_info[customer][contact]}&lt;br /&gt;
  {/section}
{/section}

  </pre>
<p>
   The above example will output:
  </p>
<pre class="screen">

&lt;hr&gt;
  id: 1000&lt;br /&gt;
  name: John Smith&lt;br /&gt;
  address: 253 N 45th&lt;br /&gt;
    home phone: 555-555-5555&lt;br /&gt;
    cell phone: 666-555-5555&lt;br /&gt;
    e-mail: john@myexample.com&lt;br /&gt;
&lt;hr&gt;
  id: 1001&lt;br /&gt;
  name: Jack Jones&lt;br /&gt;
  address: 417 Mulberry ln&lt;br /&gt;
    home phone: 123-456-4&lt;br /&gt;
    web: www.example.com&lt;br /&gt;
&lt;hr&gt;
  id: 1002&lt;br /&gt;
  name: Jane Munson&lt;br /&gt;
  address: 5605 apple st&lt;br /&gt;
    cell phone: 0457878&lt;br /&gt;

  </pre>
</div>
</div>
<br class="example-break"><div class="example">
<a name="id379496"></a><p class="title"><b>Example 7.69. Database example with a {sectionelse}</b></p>
<div class="example-contents">
<p>Results of a database search (eg ADODB or PEAR) are assigned to Smarty</p>
<pre class="programlisting">
  
&lt;?php
$sql = 'select id, name, home, cell, email from contacts '
      ."where name like '$foo%' ";
$smarty-&gt;assign('contacts', $db-&gt;getAll($sql));
?&gt;

</pre>
<p>The template to output the database result in a HTML table</p>
<pre class="programlisting">

&lt;table&gt;
&lt;tr&gt;&lt;th&gt;&amp;nbsp;&lt;/th&gt;&lt;th&gt;Name&gt;&lt;/th&gt;&lt;th&gt;Home&lt;/th&gt;&lt;th&gt;Cell&lt;/th&gt;&lt;th&gt;Email&lt;/th&gt;&lt;/tr&gt;
{section name=co loop=$contacts}
  &lt;tr&gt;
    &lt;td&gt;&lt;a href="view.php?id={$contacts[co].id}"&gt;view&lt;a&gt;&lt;/td&gt;
    &lt;td&gt;{$contacts[co].name}&lt;/td&gt;
    &lt;td&gt;{$contacts[co].home}&lt;/td&gt;
    &lt;td&gt;{$contacts[co].cell}&lt;/td&gt;
    &lt;td&gt;{$contacts[co].email}&lt;/td&gt;
  &lt;tr&gt;
{sectionelse}
  &lt;tr&gt;&lt;td colspan="5"&gt;No items found&lt;/td&gt;&lt;/tr&gt;
{/section}
&lt;/table&gt;

</pre>
</div>
</div>
<br class="example-break"><div class="sect2" title=".index">
<div class="titlepage"><div><div><h3 class="title">
<a name="section.property.index"></a>.index</h3></div></div></div>
<p>
   <em class="parameter"><code>index</code></em> contains the current array index, starting with zero
   or the <em class="parameter"><code>start</code></em> attribute if given. It increments by one or by
   the <em class="parameter"><code>step</code></em> attribute if given.
  </p>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
    If the <em class="parameter"><code>step</code></em> and <em class="parameter"><code>start</code></em>
     properties are not
    modified, then this works the same as the <a class="link" href="language.function.section.html#section.property.iteration" title=".iteration"><em class="parameter"><code>iteration</code></em></a>
    property, except it starts at zero instead of one.
   </p>
</div>
<div class="example">
<a name="id379605"></a><p class="title"><b>Example 7.70. {section} <code class="varname">index</code> property</b></p>
<div class="example-contents">
<p>
</p>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p><code class="literal">$custid[customer.index]</code> and
<code class="literal">$custid[customer]</code> are identical.</p>
</div>
<p>
</p>
<pre class="programlisting">

{section name=customer loop=$custid}
  {$smarty.section.customer.index} id: {$custid[customer]}&lt;br /&gt;
{/section}

  </pre>
<p>
   The above example will output:
  </p>
<pre class="screen">

0 id: 1000&lt;br /&gt;
1 id: 1001&lt;br /&gt;
2 id: 1002&lt;br /&gt;

   </pre>
</div>
</div>
<br class="example-break">
</div>
<div class="sect2" title=".index_prev">
<div class="titlepage"><div><div><h3 class="title">
<a name="section.property.index.prev"></a>.index_prev</h3></div></div></div>
<p>
   <em class="parameter"><code>index_prev</code></em> is the previous loop index.
   On the first loop, this is set to -1.
  </p>
</div>
<div class="sect2" title=".index_next">
<div class="titlepage"><div><div><h3 class="title">
<a name="section.property.index.next"></a>.index_next</h3></div></div></div>
<p>
   <em class="parameter"><code>index_next</code></em> is  the next loop index. On the last
   loop, this is still one more than the current index, respecting the
   setting of the <em class="parameter"><code>step</code></em> attribute, if given.
  </p>
<div class="example">
<a name="id379702"></a><p class="title"><b>Example 7.71. <code class="varname">index</code>, <code class="varname">index_next</code>
 and <code class="varname">index_prev</code> properties </b></p>
<div class="example-contents">
<pre class="programlisting">

&lt;?php
$data = array(1001,1002,1003,1004,1005);
$smarty-&gt;assign('rows',$data);
?&gt;

</pre>
<p>Template to output the above array in a table</p>
<pre class="programlisting">

{* $rows[row.index] and $rows[row] are identical in meaning *}
&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;index&lt;/th&gt;&lt;th&gt;id&lt;/th&gt;
    &lt;th&gt;index_prev&lt;/th&gt;&lt;th&gt;prev_id&lt;/th&gt;
    &lt;th&gt;index_next&lt;/th&gt;&lt;th&gt;next_id&lt;/th&gt;
  &lt;/tr&gt;
{section name=row loop=$rows}
  &lt;tr&gt;
    &lt;td&gt;{$smarty.section.row.index}&lt;/td&gt;&lt;td&gt;{$rows[row]}&lt;/td&gt;
    &lt;td&gt;{$smarty.section.row.index_prev}&lt;/td&gt;&lt;td&gt;{$rows[row.index_prev]}&lt;/td&gt;
    &lt;td&gt;{$smarty.section.row.index_next}&lt;/td&gt;&lt;td&gt;{$rows[row.index_next]}&lt;/td&gt;
  &lt;/tr&gt;
{/section}
&lt;/table&gt;

  </pre>
<p>
 The above example will output a table containing the  following:
  </p>
<pre class="screen">

index  id    index_prev prev_id index_next next_id
0      1001  -1	                1          1002
1      1002  0          1001    2          1003
2      1003  1          1002    3          1004
3      1004  2          1003    4          1005
4      1005  3          1004    5

   </pre>
</div>
</div>
<br class="example-break">
</div>
<div class="sect2" title=".iteration">
<div class="titlepage"><div><div><h3 class="title">
<a name="section.property.iteration"></a>.iteration</h3></div></div></div>
<p>
   <em class="parameter"><code>iteration</code></em> contains the current loop iteration and starts at one.
  </p>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
    This is not affected by the <code class="varname">{section}</code> properties
    <em class="parameter"><code>start</code></em>, <em class="parameter"><code>step</code></em> and <em class="parameter"><code>max</code></em>,
     unlike the <a class="link" href="language.function.section.html#section.property.index" title=".index"><em class="parameter"><code>index</code></em></a>
    property. <em class="parameter"><code>iteration</code></em> also starts with one instead of zero
    unlike <em class="parameter"><code>index</code></em>. <a class="link" href="language.function.section.html#section.property.rownum" title=".rownum"><em class="parameter"><code>rownum</code></em></a> is an alias to
    <em class="parameter"><code>iteration</code></em>,  they are identical.
   </p>
</div>
<div class="example">
<a name="id379845"></a><p class="title"><b>Example 7.72. A section's <code class="varname">iteration</code> property </b></p>
<div class="example-contents">
<pre class="programlisting">

&lt;?php
// array of 3000 to 3015
$id = range(3000,3015);
$smarty-&gt;assign('arr',$id);
?&gt;

</pre>
<p>Template to output every other element of the <code class="literal">$arr</code>
array as <code class="literal">step=2</code></p>
<pre class="programlisting">

{section name=cu loop=$arr start=5 step=2}
  iteration={$smarty.section.cu.iteration}
  index={$smarty.section.cu.index}
  id={$custid[cu]}&lt;br /&gt;
{/section}

  </pre>
<p>
   The above example will output:
  </p>
<pre class="screen">

iteration=1 index=5 id=3005&lt;br /&gt;
iteration=2 index=7 id=3007&lt;br /&gt;
iteration=3 index=9 id=3009&lt;br /&gt;
iteration=4 index=11 id=3011&lt;br /&gt;
iteration=5 index=13 id=3013&lt;br /&gt;
iteration=6 index=15 id=3015&lt;br /&gt;

   </pre>
<p>
   Another example that uses the <em class="parameter"><code>iteration</code></em> property to
   output a table header block every five rows.
  </p>
<pre class="programlisting">

&lt;table&gt;
{section name=co loop=$contacts}
  {if $smarty.section.co.iteration is div by 5}
    &lt;tr&gt;&lt;th&gt;&amp;nbsp;&lt;/th&gt;&lt;th&gt;Name&gt;&lt;/th&gt;&lt;th&gt;Home&lt;/th&gt;&lt;th&gt;Cell&lt;/th&gt;&lt;th&gt;Email&lt;/th&gt;&lt;/tr&gt;
  {/if}
  &lt;tr&gt;
    &lt;td&gt;&lt;a href="view.php?id={$contacts[co].id}"&gt;view&lt;a&gt;&lt;/td&gt;
    &lt;td&gt;{$contacts[co].name}&lt;/td&gt;
    &lt;td&gt;{$contacts[co].home}&lt;/td&gt;
    &lt;td&gt;{$contacts[co].cell}&lt;/td&gt;
    &lt;td&gt;{$contacts[co].email}&lt;/td&gt;
  &lt;tr&gt;
{/section}
&lt;/table&gt;

  </pre>
<p>
     An that uses the <em class="parameter"><code>iteration</code></em> property to
     alternate a text color every third row.
    </p>
<pre class="programlisting">
  
  &lt;table&gt;
  {section name=co loop=$contacts}
    {if $smarty.section.co.iteration is even by 3}
      &lt;span style="color: #ffffff"&gt;{$contacts[co].name}&lt;/span&gt;
    {else}
      &lt;span style="color: #dddddd"&gt;{$contacts[co].name}&lt;/span&gt;
    {/if}
  {/section}
  &lt;/table&gt;
  
    </pre>
</div>
</div>
<br class="example-break"><div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
  The <span class="emphasis"><em>"is div by"</em></span> syntax is a simpler alternative to the PHP mod operator syntax. The mod operator is allowed: <code class="literal">{if $smarty.section.co.iteration % 5 == 1}</code> will work just the same.
</p>
</div>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
  You can also use <span class="emphasis"><em>"is odd by"</em></span> to reverse the alternating.
</p>
</div>
</div>
<div class="sect2" title=".first">
<div class="titlepage"><div><div><h3 class="title">
<a name="section.property.first"></a>.first</h3></div></div></div>
<p>
   <em class="parameter"><code>first</code></em> is set to <code class="constant">TRUE</code> if the current
   <code class="varname">{section}</code> iteration is the initial one.
  </p>
</div>
<div class="sect2" title=".last">
<div class="titlepage"><div><div><h3 class="title">
<a name="section.property.last"></a>.last</h3></div></div></div>
<p>
   <em class="parameter"><code>last</code></em> is set to <code class="constant">TRUE</code>
   if the current section iteration is the final one.
  </p>
<div class="example">
<a name="id380017"></a><p class="title"><b>Example 7.73. {section} property <code class="varname">first</code> and <code class="varname">last</code></b></p>
<div class="example-contents">
<p>
   This example loops the <code class="varname">$customers</code> array,
   outputs a header block on the first iteration and
   on the last outputs the footer block. Also uses the
   <a class="link" href="language.function.section.html#section.property.total" title=".total"><em class="parameter"><code>total</code></em></a> property.
   </p>
<pre class="programlisting">

{section name=customer loop=$customers}
  {if $smarty.section.customer.first}
    &lt;table&gt;
    &lt;tr&gt;&lt;th&gt;id&lt;/th&gt;&lt;th&gt;customer&lt;/th&gt;&lt;/tr&gt;
  {/if}

  &lt;tr&gt;
    &lt;td&gt;{$customers[customer].id}}&lt;/td&gt;
    &lt;td&gt;{$customers[customer].name}&lt;/td&gt;
  &lt;/tr&gt;

  {if $smarty.section.customer.last}
    &lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;{$smarty.section.customer.total} customers&lt;/td&gt;&lt;/tr&gt;
    &lt;/table&gt;
  {/if}
{/section}

  </pre>
</div>
</div>
<br class="example-break">
</div>
<div class="sect2" title=".rownum">
<div class="titlepage"><div><div><h3 class="title">
<a name="section.property.rownum"></a>.rownum</h3></div></div></div>
<p>
   <em class="parameter"><code>rownum</code></em> contains the current loop iteration,
   starting with one. It is an alias to <a class="link" href="language.function.section.html#section.property.iteration" title=".iteration"><em class="parameter"><code>iteration</code></em></a>,
   they work identically.
  </p>
</div>
<div class="sect2" title=".loop">
<div class="titlepage"><div><div><h3 class="title">
<a name="section.property.loop"></a>.loop</h3></div></div></div>
<p>
   <em class="parameter"><code>loop</code></em> contains the last index number
   that this {section}
   looped. This can be used inside or after the <code class="varname">{section}</code>.
     </p>
<div class="example">
<a name="id380114"></a><p class="title"><b>Example 7.74. {section} property <code class="varname">loop</code></b></p>
<div class="example-contents">
<pre class="programlisting">

{section name=customer loop=$custid}
  {$smarty.section.customer.index} id: {$custid[customer]}&lt;br /&gt;
{/section}
There are {$smarty.section.customer.loop} customers shown above.

  </pre>
<p>
   The above example will output:
  </p>
<pre class="screen">

0 id: 1000&lt;br /&gt;
1 id: 1001&lt;br /&gt;
2 id: 1002&lt;br /&gt;
There are 3 customers shown above.

   </pre>
</div>
</div>
<br class="example-break">
</div>
<div class="sect2" title=".show">
<div class="titlepage"><div><div><h3 class="title">
<a name="section.property.show"></a>.show</h3></div></div></div>
<p>
   <em class="parameter"><code>show</code></em> is used as a parameter to section and is
   a boolean value. If
   <code class="constant">FALSE</code>, the section will not be displayed. If there is a
   <code class="varname">{sectionelse}</code>  present, that will be alternately displayed.
  </p>
<div class="example">
<a name="id380170"></a><p class="title"><b>Example 7.75. <code class="varname">show</code> property </b></p>
<div class="example-contents">
<p>Boolean <code class="varname">$show_customer_info</code> has been passed from the PHP
  application, to regulate whether or not this section shows.</p>
<pre class="programlisting">

{section name=customer loop=$customers show=$show_customer_info}
  {$smarty.section.customer.rownum} id: {$customers[customer]}&lt;br /&gt;
{/section}

{if $smarty.section.customer.show}
  the section was shown.
{else}
  the section was not shown.
{/if}

  </pre>
<p>
   The above example will output:
  </p>
<pre class="screen">

1 id: 1000&lt;br /&gt;
2 id: 1001&lt;br /&gt;
3 id: 1002&lt;br /&gt;

the section was shown.

   </pre>
</div>
</div>
<br class="example-break">
</div>
<div class="sect2" title=".total">
<div class="titlepage"><div><div><h3 class="title">
<a name="section.property.total"></a>.total</h3></div></div></div>
<p>
   <em class="parameter"><code>total</code></em> contains the number of iterations that this
   <code class="varname">{section}</code> will loop. This can be used inside or after a
   <code class="varname">{section}</code>.
  </p>
<div class="example">
<a name="id380234"></a><p class="title"><b>Example 7.76. <code class="varname">total</code> property example</b></p>
<div class="example-contents"><pre class="programlisting">

{section name=customer loop=$custid step=2}
  {$smarty.section.customer.index} id: {$custid[customer]}&lt;br /&gt;
{/section}
   There are {$smarty.section.customer.total} customers shown above.

  </pre></div>
</div>
<br class="example-break"><p>
   See also <a class="link" href="language.function.foreach.html" title="{foreach},{foreachelse}"><code class="varname">{foreach}</code></a>,
   <a class="link" href="language.function.for.html" title="{for}"><code class="varname">{for}</code></a>,
   <a class="link" href="language.function.while.html" title="{while}"><code class="varname">{while}</code></a>
    and
   <a class="link" href="language.variables.smarty.html#language.variables.smarty.loops" title="{$smarty.section}"><em class="parameter"><code>$smarty.section</code></em></a>.
  </p>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="language.function.php.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="language.builtin.functions.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="language.function.setfilter.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">{php} </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> {setfilter}</td>
</tr>
</table>
</div>
</body>
</html>
